package com.study.manage.mvc.org.finance;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.converter.Converter;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.study.manage.dal.EvaluateApplyRepository;
import com.study.manage.dal.EvaluateBrandOrgRecommendRepository;
import com.study.manage.dal.EvaluateGroupDetailRepository;
import com.study.manage.dal.EvaluateRepository;
import com.study.manage.dal.OrderRepository;
import com.study.manage.domain.Evaluate;
import com.study.manage.domain.enumm.LiveDieState;
import com.study.manage.dto.AjaxResponse;
import com.study.manage.dto.SessionUser;
import com.study.manage.mvc.common.BaseController;
import com.study.manage.utils.MapUtils;

@Controller
@RequestMapping("org/acc")
public class FinanceController extends BaseController {

    @Autowired
    private EvaluateRepository evaluateRepository;
    @Autowired
    private OrderRepository orderRepository;
    @Autowired
    private EvaluateApplyRepository evaluateApplyRepository;
    @Autowired
    private EvaluateGroupDetailRepository evaluateGroupDetailRepository;
    @Autowired
    private EvaluateBrandOrgRecommendRepository evaluateBrandOrgRecommendRepository;

    @RequestMapping(path = "finance", method = RequestMethod.GET)
    public String info(Model model) {
        SessionUser su = (SessionUser) session.getAttribute("user");
        // model.addAttribute("identityFlg", su.getIdentityFlg());
        return "org/evaluate/finance";
    }

    @ResponseBody
    @RequestMapping(path = "getTableData", method = RequestMethod.GET)
    public AjaxResponse info_list(
            @PageableDefault(sort = {"createdAt"}, direction = Direction.DESC) Pageable pageable) {

        AjaxResponse ajax = new AjaxResponse(true);
        SessionUser su = (SessionUser) session.getAttribute("user");
        List<Long> eIdList = evaluateBrandOrgRecommendRepository.findByUserIdAndState(su.getId(),
                LiveDieState.LIVE);
        Page<Evaluate> page = evaluateRepository.findByidIn(eIdList, pageable);
        Object obj = page.map(new Converter<Evaluate, Map<String, Object>>() {
            public Map<String, Object> convert(Evaluate source) {
                Map<String, Object> map = MapUtils.convert(source);
                Object obj = evaluateGroupDetailRepository.getFinanceDatas(source.id);
                Object[] o1 = (Object[]) obj;
                map.put("groupName", (String) o1[0]);
                map.put("organizationName", (String) o1[1]);
                map.put("applyNumber",
                        evaluateApplyRepository.countByEvaluateIdAndDay(source.id, null));
                map.put("orderSum",
                        orderRepository.sumPriceByEvaluateIdIn(Arrays.asList(source.id), null));
                return map;
            }
        });
        return ajax.setObj(obj);
    }


    @ResponseBody
    @RequestMapping(path = "info-detail-count", method = RequestMethod.GET)
    public AjaxResponse list_detail_count() {

        AjaxResponse ajax = new AjaxResponse(true);
        Map<String, Object> map = new HashMap<>();
        map.put("totalMoney", "100.00");
        map.put("quickMoney", "100.00");
        return ajax.setObj(map);
    }

}


